import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;

/**
 * Created with IntelliJ IDEA.
 * Description:修改
 * User: 28463
 * Date: 2022—08—17
 * Time: 15:55
 */
public class libraryUpdate {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");

        //2.连接数据库
        Connection connection = dataSource.getConnection();

        //3.构造sql语句
        String sql = "select book.name,student.name,borrow_info.start_time,borrow_info.end_time from category " +
                "join book on category.id = book.category_id " +
                "join borrow_info on book.id = borrow_info.book_id " +
                "join student on student.id = borrow_info.student_id " +
                "where category.name = '计算机'";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行SQL语句
        ResultSet resultSet = statement.executeQuery();

        //5.遍历结果集合
        while (resultSet.next()) {
            String bookName = resultSet.getString("book.name");
            String studentName = resultSet.getString("student.name");
            Timestamp startTime = resultSet.getTimestamp("borrow_info.start_time");
            Timestamp endTime = resultSet.getTimestamp("borrow_info.end_time");
            System.out.println("book.name = " + bookName + "student.name = " + studentName + "borrow_info.start_time = " + studentName + "borrow_info.end_time" + endTime);
        }
        //6.释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
